package com.example.ycdyus.dailyfruit.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.example.ycdyus.dailyfruit.constant.AppConfig;
import com.loopj.android.http.AsyncHttpClient;

/**
 * Created by ycdyus on 2015/9/22.
 */
public class Utils {
    public static final String isFirstKey = "isFirst";
    public static final String versinKey = "version";
    private static AsyncHttpClient client;
    //判断是否是第一次进入
    public static boolean isFirst(Context context){
        boolean flag = false;
        int version = 1;
        SharedPreferences preferences = context.getSharedPreferences(AppConfig.ISFRTST,Context.MODE_PRIVATE);
        flag = preferences.getBoolean(isFirstKey,true);
        version = preferences.getInt(versinKey,1);
        if(flag || version != getVersion(context)){
            preferences.edit().putBoolean(isFirstKey,false).commit();
            preferences.edit().putInt(versinKey,getVersion(context)).commit();
            return true;
        }
        return false;
    }
    //获取版本信息
    public static int getVersion(Context context){
        int version = 1;
        PackageManager manager = context.getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(),0);
            version = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }
    /**
     *从网络加载数据进行版本检测
     */
//    public  static void checkVersion(final Context context) {
//        client = new AsyncHttpClient();
//        RequestParams params = new RequestParams();
//        params.add("platform", "2");
//        params.add("ver", "v" + Utils.getVersion(context));
//        client.post(URLConstant.APK_UPDATE, params, new TextHttpResponseHandler() {
//            @Override
//            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
//
//            }
//
//            @Override
//            public void onSuccess(int statusCode, Header[] headers, String responseString) {
//                if (responseString != null) {
//                    try {
//                        JSONObject o = new JSONObject(responseString);
//                        JSONObject oo = o.getJSONObject("info");
//                        String version = oo.getString("ver");
//                        String path = oo.getString("src");
//                        showDialog(version, oo.getString("msg"), context,path);
//                    } catch (JSONException e) {
//                        BaseApp.showToast("HHHHH");
//                        e.printStackTrace();
//                    }
//                }
//            }
//        });
//    }

//    private static void showDialog(String version,String msg, final Context context, final String path) {
//        AlertDialog.Builder builder=new AlertDialog.Builder(context);
//        builder.setTitle(version);
//        builder.setMessage(msg);
//        builder.setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                new DownApkAsync(context).execute(path);
//            }
//        });
//        builder.setNegativeButton("稍后再说", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//
//            }
//        });
//        builder.show();
//    }
}
